﻿Imports System.Collections
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Imports Tohto.Base.ExceptionPolicy.Config
Imports Tohto.Base.ExceptionPolicy.Interfaces
Imports Tohto.Base.ExceptionPolicy.MessageHandler
Imports System.Reflection
Imports System.Xml

<Serializable()>
Public Class ExPolicy
    Private Shared ConfigCollection As ExceptionConfig = GetConfig()

    Private Shared IsLoadHanlder As Boolean = False
    Private Shared htbExHandlers As New Hashtable()

    Private Shared objErrorInfo As New ErrorInfo(ConfigCollection)

    Public Sub New()
        If Not IsLoadHanlder Then
            GetHandlers()
        End If
    End Sub

    Private Sub GetHandlers()
        For Each configHandler As ExceptionConfig.ExceptionHandlerConfig In ConfigCollection
            Dim objHandler As ExceptionHandlerBase = TryCast(System.Activator.CreateInstance(Type.[GetType](configHandler.Type)), ExceptionHandlerBase)
            objHandler.LoadLogHandler(configHandler)
            htbExHandlers.Add(configHandler.Name, objHandler)
        Next

        'htbExHandlers.Add("Default", objHandler);
        If htbExHandlers.Count = 0 Then
        End If

        IsLoadHanlder = True
    End Sub

    Public Function ProcessExeception(ByVal ex As Exception, ByVal strErrorCode As String, ByVal strExHanlderName As String, ByVal strUserId As String) As String
        Dim exHandler As ExceptionHandlerBase
        Dim strException As String

        Try
            exHandler = DirectCast(htbExHandlers(strExHanlderName), ExceptionHandlerBase)
        Catch e As Exception
            Throw New Exception("Exceptionが存在しません、Configファイルを確認してください。", e)
        End Try

        strException = exHandler.ProcessExeception(ex, strUserId, strErrorCode)

        Dim message As IMessage = Nothing
        Select Case exHandler.ExHanlderConfig.AlertType
            Case ExceptionAlertType.WinUI
                If True Then
                    message = New MessageHandler.WinFormMessage()
                    Exit Select
                End If
            Case ExceptionAlertType.WEBUI
                If True Then
                    message = New MessageHandler.WebMessage()
                    Exit Select
                End If
            Case ExceptionAlertType.None
                If True Then
                    Exit Select
                End If
            Case Else
                If True Then
                    Exit Select
                End If
        End Select
        If message IsNot Nothing Then
            message.ShowMessage(strException)
        End If
        Return strException
    End Function

    Public Shared Function GetErrorInfoByCode(ByVal strErrorCode As String) As String
        Return objErrorInfo.GetErrorInfoById(strErrorCode)
    End Function

    Private Shared Function GetConfig() As ExceptionConfig

        Dim asse As Assembly = Assembly.GetExecutingAssembly()
        Dim stream As IO.Stream = Nothing
        Try

            Dim strNames As String() = asse.GetManifestResourceNames()
            For Each strName As String In strNames
                If strName = "Tohto.Base.ExceptionPolicy.ExceptionConfig.xml" OrElse strName = "ExceptionConfig.xml" Then
                    stream = asse.GetManifestResourceStream(strName)
                End If
            Next
        Catch ex As Exception
            Throw ex
        End Try

        Dim xmlDoc As New XmlDocument()
        xmlDoc.Load(stream)

        Dim handler As New ExceptionSectionHandler()
        Return TryCast(handler.Create(Nothing, Nothing, xmlDoc.SelectSingleNode("/ExceptionPolicy")), ExceptionConfig)
    End Function

End Class
